package cn.lyw.init;


import cn.common.init.InitDataConstant;
import cn.common.redis.RedisServie;
import cn.lyw.seckillgoods.service.bo.GetSeckillGoodsBySessionBO;
import cn.lyw.seckillgoods.service.domain.GetSeckillGoodsBySessionDO;
import cn.lyw.seckillgoods.service.service.SeckillGoodsService;
import cn.lyw.seckillsession.service.domain.GetSessionTodayDO;
import cn.lyw.seckillsession.service.service.SeckillSessionService;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author   lyw
 * @Description 秒杀模块初始化
 * @Date 14:35 2021/7/24
 * @Param
 * @return
 */
@Configuration
public class SeckillInit implements InitializingBean {

    Logger logger = LoggerFactory.getLogger(SeckillInit.class);

    @Autowired
    private SeckillSessionService seckillSessionService;

    @Autowired
    private SeckillGoodsService seckillGoodsService;

    @Autowired
    private RedisServie redisServie;


    @Override
    public void afterPropertiesSet() throws Exception {

        System.out.println("初始化开始");
        initSeckill();
        System.out.println("初始化结束");
    }


    /**
     * @Author   lyw
     * @Description 初始化当天秒杀商品
     * @Date 15:11 2021/7/24
     * @Param
     * @return
     */
    public void initSeckill(){

        //查询当天的秒杀场次
        List<GetSessionTodayDO> sessionToday = seckillSessionService.getSessionToday();


        for (GetSessionTodayDO getSessionTodayDO : sessionToday) {

            //未开始的
            if (getSessionTodayDO.getSessionState().equals("seckillNotStart")){

                //获取时间
                long time = getSessionTodayDO.getStartTime().getTime();
                long time1 = (new Date()).getTime();
                long l = (time - time1)/1000;

                //存入redis
                redisServie.putHash(InitDataConstant.SECKILL_SESSION_SECKILLNOTSTART+getSessionTodayDO.getSessionId(),getSessionTodayDO.getSessionId(),getSessionTodayDO,l);

                //通过场次id查询秒杀商品
                GetSeckillGoodsBySessionBO getSeckillGoodsBySessionBO = new GetSeckillGoodsBySessionBO();
                getSeckillGoodsBySessionBO.setSeckillSessionId(getSessionTodayDO.getSessionId());
                List<GetSeckillGoodsBySessionDO> seckillGoodsBySession = seckillGoodsService.getSeckillGoodsBySession(getSeckillGoodsBySessionBO);

                //将商品存入redis
                redisServie.setList(InitDataConstant.SECKILL_GOODS+getSessionTodayDO.getSessionId(),seckillGoodsBySession);
            }


            //活动中的
            if (getSessionTodayDO.getSessionState().equals("seckillActivity")){

                //获取时间
                long time = getSessionTodayDO.getEndTime().getTime();
                long time1 = (new Date()).getTime();
                long l = (time - time1)/1000;

                //存入redis
                redisServie.putHash(InitDataConstant.SECKILL_SESSION_SECKILLACTIVITY+getSessionTodayDO.getSessionId(),getSessionTodayDO.getSessionId(),getSessionTodayDO,l);

                //通过场次id查询秒杀商品
                GetSeckillGoodsBySessionBO getSeckillGoodsBySessionBO = new GetSeckillGoodsBySessionBO();
                getSeckillGoodsBySessionBO.setSeckillSessionId(getSessionTodayDO.getSessionId());
                List<GetSeckillGoodsBySessionDO> seckillGoodsBySession = seckillGoodsService.getSeckillGoodsBySession(getSeckillGoodsBySessionBO);

                //将商品存入redis
                redisServie.setList(InitDataConstant.SECKILL_GOODS+getSessionTodayDO.getSessionId(),seckillGoodsBySession);

            }

        }

    }

}
